package com.yingluo.abacus.cloud.service.web;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.yingluo.abacus.cloud.service.web.utils.ServiceWebUtils;

//http://localhost:8081/abacus/cloud/service-web/rest-template
@RestController
@RequestMapping(value =  ServiceWebUtils.MODULE_SERIVCEWEB)
public class RestTemplateControler {
    private static final Logger logger = LoggerFactory.getLogger(RestTemplateControler.class);


    @Autowired
    RestTemplateService service;

    @RequestMapping(value = "/rest-template")
    public String restTemplate() {

        String str = service.hiService();

        logger.debug("======" + str);
        return str;
    }

}

@Service
class RestTemplateService {

    @Value("${spring.application.host-service}")
    String hostService;

    @Autowired
    RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "hiError")
    public String hiService() {
        return restTemplate.getForObject("http://" + hostService + ServiceWebUtils.MODULE_SERIVCEPROVIDER + "/hello", String.class);
    }

    public String hiError() {
        return "sorry, cloud-service-web was error ,Hystrix !";
    }
}
